Skip to contentMethod: contextMatches(Assertion, Statement)
1: package cz.cvut.kbss.ontodriver.sesame;
2:
3: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
4: import cz.cvut.kbss.ontodriver.model.Assertion;
5: import cz.cvut.kbss.ontodriver.model.Axiom;
6: import cz.cvut.kbss.ontodriver.sesame.config.Constants;
7: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
8: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
9: import cz.cvut.kbss.ontodriver.sesame.util.AxiomBuilder;
10: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
11: import org.openrdf.model.Resource;
12: import org.openrdf.model.Statement;
13: import org.openrdf.model.URI;
14: import org.openrdf.model.ValueFactory;
15:
16: import java.util.Collection;
17: import java.util.HashSet;
18: import java.util.Map;
19:
20: class StatementLoader {
21:
22: private final AxiomDescriptor descriptor;
23: private final Connector connector;
24: private final Resource subject;
25: private final ValueFactory vf;
26: private final AxiomBuilder axiomBuilder;
27:
28: private boolean loadAll;
29: private boolean includeInferred;
30:
31: StatementLoader(AxiomDescriptor descriptor, Connector connector, Resource subject, AxiomBuilder axiomBuilder) {
32: this.descriptor = descriptor;
33: this.connector = connector;
34: this.vf = connector.getValueFactory();
35: this.subject = subject;
36: this.axiomBuilder = axiomBuilder;
37: }
38:
39: void setIncludeInferred(boolean includeInferred) {
40: this.includeInferred = includeInferred;
41: }
42:
43: Collection<Axiom<?>> loadAxioms(Map<URI, Assertion> properties)
44: throws SesameDriverException {
45: this.loadAll = properties.values().contains(Assertion.createUnspecifiedPropertyAssertion(includeInferred));
46: if (properties.size() < Constants.DEFAULT_LOAD_ALL_THRESHOLD && !loadAll) {
47: return loadOneByOne(properties.values());
48: } else {
49: return loadAll(properties);
50: }
51: }
52:
53: private Collection<Axiom<?>> loadOneByOne(Collection<Assertion> assertions) throws SesameDriverException {
54: final Collection<Axiom<?>> result = new HashSet<>();
55: for (Assertion a : assertions) {
56: final URI context = SesameUtils.toSesameUri(descriptor.getAssertionContext(a), vf);
57: final URI property = SesameUtils.toSesameUri(a.getIdentifier(), vf);
58:
59: final Collection<Statement> statements;
60: if (context != null) {
61: statements = connector.findStatements(subject, property, null, includeInferred, context);
62: } else {
63: statements = connector.findStatements(subject, property, null, includeInferred);
64: }
65: for (Statement s : statements) {
66: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s, a);
67: if (axiom != null) {
68: result.add(axiom);
69: }
70: }
71: }
72: return result;
73: }
74:
75: private Collection<Axiom<?>> loadAll(Map<URI, Assertion> properties) throws SesameDriverException {
76: final Collection<Statement> statements = connector.findStatements(subject, null, null, includeInferred);
77: final Collection<Axiom<?>> result = new HashSet<>(statements.size());
78: final Assertion unspecified = Assertion.createUnspecifiedPropertyAssertion(includeInferred);
79: for (Statement s : statements) {
80: if (!properties.containsKey(s.getPredicate()) && !loadAll) {
81: continue;
82: }
83: final Assertion a = getAssertion(properties, s);
84: if (!contextMatches(a, s) && !(loadAll && contextMatches(unspecified, s))) {
85: continue;
86: }
87: final Axiom<?> axiom = axiomBuilder.statementToAxiom(s);
88: if (axiom != null) {
89: result.add(axiom);
90: }
91: }
92: return result;
93: }
94:
95: private Assertion getAssertion(Map<URI, Assertion> properties, Statement s) {
96: if (properties.containsKey(s.getPredicate())) {
97: return properties.get(s.getPredicate());
98: }
99: return Assertion.createUnspecifiedPropertyAssertion(includeInferred);
100: }
101:
102: private boolean contextMatches(Assertion a, Statement s) {
103: final java.net.URI assertionCtx = descriptor.getAssertionContext(a);
104: final Resource statementContext = s.getContext();
105:• if (assertionCtx == null) {
106: // If the assertion should be in default, we don't care about the context of the statement, because
107: // the default is a union of all the contexts
108: return true;
109: }
110:• return statementContext != null && assertionCtx.toString().equals(statementContext.stringValue());
111: }
112: }